package com.vgooo.util;


import com.vgooo.client.R;
import com.vgooo.client.VGActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

public class Loading {
		
	public static Progress startProgress(Activity context){
		Progress progress = new Progress();
		View view = LayoutInflater.from(context).inflate(R.layout.loading, null);
		ImageView image = (ImageView) view.findViewById(R.id.loading_image);
		context.addContentView(view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		progress.startAnimation(view, image, context);
		return progress;
	}
	
	public static Progress startProgress(Activity context,View view, View image){
		Progress progress = new Progress();
		progress.startAnimation(view, image, context);
		return progress;
	}
	
	 public static class Progress{
		private  LoadingTimeTash lading ;
		private Animation anim ;
		private View view;
		private View image;
		private final static int START = 789 ;
		private final static int STOP = 790;
		private Activity context ;
		private Handler handler ;
		
		private void startAnimation(final View view, final View image,Activity context ){
			
			if (view == null)
			{
				this.view = image;
			}
			else 
			{
				this.view = view;
			}
			this.image = image;
			anim = AnimationUtils.loadAnimation(context,R.anim.loading);
			LinearInterpolator lir = new LinearInterpolator();
			anim.setInterpolator(lir);
			this.context = context ;
			handler = new Handler(){
				public void handleMessage(android.os.Message msg) {
					switch (msg.what) {
					case START:
						image.startAnimation(anim);
						break;

					default:
						break;
					}
				};
			};
			lading = new LoadingTimeTash(handler,context);
			lading.start();
		}
		
		public void start(){
			lading = new LoadingTimeTash(handler,context);
			view.setVisibility(View.VISIBLE);
			lading.start();
		}
		
		public  void cancel(){
			if(lading ==null){
				return ;
			}
			lading.setLoading(false);
			if (view != null)
			{
				view.setVisibility(View.GONE);
			}
		}
		
		private static class LoadingTimeTash extends Thread{

			private boolean isLoading ;
			private Handler handler ;
			private Context context ;
			
			
			public LoadingTimeTash(Handler handler,Context context) {
				super();
				this.context = context ;
				this.handler = handler ;
				this.isLoading = true ;
			}

			
			public void setLoading(boolean isLoading) {
				this.isLoading = isLoading;
			}


			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(isLoading){
					handler.sendEmptyMessage(START);
					try {
						this.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						UmengUtils.onError(context, e.toString());
						e.printStackTrace();
					}
				}
			}
			
		}
	}

}
